package cn.net.xyan.easy.gwt.client.ui;

import cn.net.xyan.easy.gwt.client.model.TableCellDataModel;
import cn.net.xyan.easy.gwt.client.ui.cell.ButtonItemCell;
import cn.net.xyan.easy.gwt.client.ui.cell.ListCell;
import cn.net.xyan.easy.gwt.shared.StringUtils;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.uibinder.client.*;
import com.google.gwt.user.cellview.client.AbstractCellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.AbstractDataProvider;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.RangeChangeEvent;
import org.gwtbootstrap3.client.ui.Pagination;
import org.gwtbootstrap3.client.ui.Panel;
import org.gwtbootstrap3.client.ui.constants.ButtonType;
import org.gwtbootstrap3.client.ui.constants.IconType;
import org.gwtbootstrap3.client.ui.gwt.CellTable;
import org.gwtbootstrap3.extras.bootbox.client.Bootbox;

import java.util.List;

/**
 * Created by zarra on 16/3/3.
 */
public class Table extends Composite {

    int pageSize;

    @UiField(provided = true)
    CellTable<TableCellDataModel> cellTable ;

    @UiField
    Pagination cellTablePagination;

    @UiField
    Panel rootPanel;

    private SimplePager cellTablePager = new SimplePager();

    private AbstractDataProvider<TableCellDataModel> cellTableProvider ;

    private TableDelegate<TableCellDataModel> delegate;

    @UiTemplate("Table.ui.xml")
    interface Binder extends UiBinder<Widget, Table > {}

    private static Binder binder = GWT.create(Binder.class);

    @UiConstructor
    public Table(int pageSize){
        this.pageSize = pageSize;
        createCellTable();
        initWidget(binder.createAndBindUi(this));
    }

    public Table(int pageSize, TableDelegate<TableCellDataModel> delegate,AbstractDataProvider<TableCellDataModel> provider){
        this.pageSize = pageSize;
        createCellTable();
        initWidget(binder.createAndBindUi(this));

        this.cellTableProvider = provider;
        this.delegate = delegate;


        initTable(cellTable,cellTablePager,cellTablePagination,cellTableProvider);
        reloadData();
    }


    private CellTable<TableCellDataModel> createCellTable(){

        cellTable = new CellTable<>(pageSize);
        return cellTable;
    }


    public void reloadData(){
        cellTable.setVisibleRangeAndClearData(cellTable.getVisibleRange(), true);

        Timer t = new Timer() {
            @Override
            public void run() {
                cellTable.setVisibleRangeAndClearData(cellTable.getVisibleRange(), true);
                cellTablePagination.rebuild(cellTablePager);
            }
        };

        t.schedule(50);


    }


    public void initTable(){
        initTable(cellTable,cellTablePager,cellTablePagination,cellTableProvider);
    }

    public void initTable(final AbstractCellTable<TableCellDataModel> grid, final SimplePager pager, final Pagination pagination,final AbstractDataProvider<TableCellDataModel> dataProvider){

        for (int i=0;i<delegate.numberOfColumn();i++){
            final int index = i;
            final Column<TableCellDataModel,?> col = delegate.column(index);
            grid.addColumn(col, delegate.columnName(i));
        }

        List<ButtonItemCell<TableCellDataModel>> buttons = delegate.buttons();

        if (buttons!= null && buttons.size()>0){
            ListCell<TableCellDataModel> listCell = new ListCell<>();
            for (ButtonItemCell<TableCellDataModel> b: buttons){
                listCell.addCell(b);
            }


            Cell<TableCellDataModel> cc = listCell.getCell();

            final Column<TableCellDataModel, TableCellDataModel> colButton = new Column<TableCellDataModel, TableCellDataModel>(
                    cc) {
                @Override
                public TableCellDataModel getValue(TableCellDataModel object) {
                    return object;
                }
            };

            grid.addColumn(colButton, "Buttons");
        }

        grid.addRangeChangeHandler(new RangeChangeEvent.Handler() {

            @Override
            public void onRangeChange(final RangeChangeEvent event) {
                pagination.rebuild(pager);
            }
        });

        pager.setDisplay(grid);
        pagination.clear();
        dataProvider.addDataDisplay(grid);
    }

    public TableDelegate<TableCellDataModel> getDelegate() {
        return delegate;
    }

    public void setDelegate(TableDelegate<TableCellDataModel> delegate) {
        this.delegate = delegate;
    }

    public CellTable<TableCellDataModel> getCellTable() {
        return cellTable;
    }

    public AbstractDataProvider<TableCellDataModel> getCellTableProvider() {
        return cellTableProvider;
    }

    public void setCellTableProvider(AbstractDataProvider<TableCellDataModel> cellTableProvider) {
        this.cellTableProvider = cellTableProvider;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}
